//
//  HomePageContentView.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/5/8.
//  Copyright © 2016年 吴可高. All rights reserved.

@interface PersonInfoModel : NSObject

@property (assign, nonatomic) CGFloat score;
+(instancetype)personInfoWithScrore:(CGFloat)score;
@end
@implementation PersonInfoModel
+(instancetype)personInfoWithScrore:(CGFloat)score;
{
    PersonInfoModel * personInfo = [[PersonInfoModel alloc]init];
    personInfo.score = score;
    return personInfo;
}
@end

#import "HomePageContentView.h"
static  NSString * kScorePath = @"score";
@interface HomePageContentView()
{
    CGRect _homePageContentViewFrame;
    UITextField * _notificationTextField;
}
@property (strong, nonatomic)   UIButton * weakSelfItemButton;
@property (strong, nonatomic) PersonInfoModel * personModel;
@property (strong, nonatomic)   NSTimer * timer;

@end

@implementation HomePageContentView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self contentViewConfig];
//        [self contentDataConfiguration];
        [self loadMyCenterContentView];
        _homePageContentViewFrame  = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), CGRectGetWidth(frame), CGRectGetHeight(frame));
    }
    return self;
}

-(void)loadMyCenterContentView
{
    _notificationTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 130, 35)];
    _notificationTextField.placeholder = @"notification_add";
    _notificationTextField.keyboardType = UIKeyboardTypeDecimalPad;
    _notificationTextField.borderStyle  = UITextBorderStyleRoundedRect;
    _notificationTextField.keyboardAppearance = UIKeyboardAppearanceDark;
    [self addSubview:_notificationTextField];
}

-(void)plateformNotificaationCenterConfig
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationTextFieldValueChanaged:) name:UITextFieldTextDidChangeNotification object:nil];
}

-(void)notificationTextFieldValueChanaged:(NSNotification*)notification
{
    
}

-(void)contentViewConfig
{
    UIButton * weakSelfButton = [UIButton buttonWithType:UIButtonTypeCustom];
     weakSelfButton.backgroundColor = [UIColor redColor];
    [weakSelfButton addTarget:self action:@selector(weakSelfItemDidSelected:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:weakSelfButton];
    _weakSelfItemButton  = weakSelfButton;
}

-(void)weakSelfItemDidSelected:(UIButton*)parameter
{
    NSLog(@"_weakSelfItemButton.count = %zd PersonInfo.count = %zd",_weakSelfItemButton.retainCount,_personModel.retainCount);
    [self.personModel removeObserver:self forKeyPath:kScorePath];
}

-(void)contentDataConfiguration
{
    PersonInfoModel *personModel = [PersonInfoModel personInfoWithScrore:100.f];
    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(personInfoScoreChanaged:) userInfo:nil repeats:YES];
    _timer = timer;
    [personModel addObserver:self forKeyPath:kScorePath options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    _personModel = personModel;
}

-(void)personInfoScoreChanaged:(NSTimer*)timer
{
    _personModel.score = arc4random()%1000 + .2f;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == nil) {
        PersonInfoModel* model = (PersonInfoModel*)object;
        NSLog(@"objec[score] = %.2f",model.score);
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


-(void)layoutSubviews
{
    [super layoutSubviews];
     self.frame = _homePageContentViewFrame;
    _weakSelfItemButton.frame = CGRectMake(CGRectGetMidX(self.frame)- CGRectGetWidth(self.frame)/4.f, CGRectGetMinY(self.frame), CGRectGetWidth(self.frame)/2.f, 30);
}

-(void)dealloc
{
    [super dealloc];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    NSLog(@"homePageContentView dealloc");
}
@end
